/**
 * 拖拽范围策略
 */
import namespace from "../../namespace";
class RegionPolicy extends namespace.policy.figure.DragDropPolicy {
  constructor(x, y, w, h) {
    super();
    if (x instanceof namespace.geo.Rectangle) {
      this.constRect = x;
    } else if (typeof h === "number") {
      this.constRect = new namespace.geo.Rectangle(x, y, w, h);
    } else {
      throw "Invalid parameter. RegionEditPolicy need a rectangle as parameter in the constructor";
    }
  }

  adjustPosition(figure, x, y) {
    var r = null;
    if (x instanceof namespace.geo.Point) {
      r = new namespace.geo.Rectangle(
        x.x,
        x.y,
        figure.getWidth(),
        figure.getHeight()
      );
    } else {
      r = new namespace.geo.Rectangle(
        x,
        y,
        figure.getWidth(),
        figure.getHeight()
      );
    }
    r = this.constRect.moveInside(r);
    return r.getTopLeft();
  }
}

namespace.policy.figure.RegionPolicy = RegionPolicy;
